
import ds from '@/datasource.js';
import { useEffect, useState } from 'react';

function useChange( selectData ){
    // 调用 selectData 获取数据
    const init = selectData( ds );
    // 声明状态并将其初始化
    const [data,setData] = useState(init);

    const handleChange = () => {
        setData( selectData( ds ) );
    }

    const effect = ()=>{
        ds.addChangeListener( handleChange );
        return function cleanup(){
            ds.removeChangeListener( handleChange );
        }
    }

    useEffect( effect ,[] );

    // 返回状态数据
    return data;
}

export default useChange ;